/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dispays;

import JAppToolKit.JLibraryLoader;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class DisplayManager {

    public static JLibraryLoader tool = null;
    private static JLibraryLoader parentloader = null;
    public static String videoLibraryClassPath = "";

    /**
     * 根据平台信息获取对应的Video2Swt类库链接
     *
     * @return
     */
    public static URL getResourceLibraryWithArch() throws MalformedURLException {
        String archs = System.getProperties().getProperty("os.arch");
        System.out.println("Arch:" + archs);
        if (archs != null && archs.contains("64")) {
            //return DisplayManager.class.getResource("/pkgfile/JVideo2SWT_64.jar");
            return new URL("file:" + videoLibraryClassPath + "/JVideo2SWT_64.jar");
        } else {
            //return DisplayManager.class.getResource("/pkgfile/JVideo2SWT_32.jar");
            return new URL("file:" + videoLibraryClassPath + "/JVideo2SWT_32.jar");
        }
    }

    /**
     * 准备类装载器
     */
    public static void readyClassLoaders()
    {
        if (parentloader == null)
        {
            String archs = System.getProperties().getProperty("os.arch");
            if (archs != null && archs.contains("64")) 
            {
                try {
                    //parentloader = new JLibraryLoader(DisplayManager.class.getResource("/pkgfile/swt_64.jar"));
                    parentloader = new JLibraryLoader(new URL("file:" + videoLibraryClassPath + "/swt_64.jar"));
                } catch (MalformedURLException ex) {
                    Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else
            {
                try {
                    //parentloader = new JLibraryLoader(DisplayManager.class.getResource("/pkgfile/swt_32.jar")); 
                    parentloader = new JLibraryLoader(new URL("file:" + videoLibraryClassPath + "/swt_32.jar"));
                } catch (MalformedURLException ex) {
                    Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //parentloader.loadAllClass(parentloader.getURLs()[0].getFile());
        }
        if (tool == null) {
            try {
                tool = new JLibraryLoader(getResourceLibraryWithArch(),parentloader);
            } catch (MalformedURLException ex) {
                Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
            }
            //tool.loadAllClass(tool.getURLs()[0].getFile());
            System.out.println("VideoLibrary:" + tool.getURLs()[0].getFile());
            System.out.println("SWTLibrary:" + parentloader.getURLs()[0].getFile());
        }
    }

    /**
     * 生成设备面板
     *
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static DeviceRecord makeVideoDevicePanel(int x, int y, int width, int height) throws MalformedURLException {
        readyClassLoaders();
        DeviceRecord panel = new DeviceRecord();
        try {
            Class cc = tool.loadClass("JVideo2SWT.JVideoWindowControl");
            java.lang.reflect.Method me = (java.lang.reflect.Method) tool.getMethodWithSameName(cc, "initVideoWindow").get(0);
            tool.invokeMethod(me, null, new Object[]{"one", x, y, width, height, true});
            //tool.invokeMethod(me,null, "JVideo2SWT.JVideoWindowControl", "initVideoWindow",new Object[]{ "one", x, y, width, height, true });
            panel.embeddedHandle = Long.parseLong(tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "getVideoHandle").toString());
            System.out.println("embeddedHandle:" + panel.embeddedHandle);
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return panel;
    }

    /**
     * 设置设备面板的大小和位置
     *
     * @param devicepanel
     * @param x
     * @param y
     * @param width
     * @param height
     */
    public static void moveCurrentDevicePanelTo(int x, int y, int width, int height) {
        readyClassLoaders();
        try {
            Class cc = tool.loadClass("JVideo2SWT.JVideoWindowControl");
            java.lang.reflect.Method me = (java.lang.reflect.Method) tool.getMethodWithSameName(cc, "setVideoLocationAndSize").get(0);
            tool.invokeMethod(me, null, new Object[]{x, y, width, height});
            //tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "setVideoLocationAndSize",new Object[]{ x, y, width, height });
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 隐藏当前设备
     */
    public static void hideCurrentDevice() {
        readyClassLoaders();
        try {
            tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "hideCurrentVideoWindow");
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 显示当前设备
     */
    public static void showCurrentDevice() {
        readyClassLoaders();
        try {
            tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "showCurrentVideoWindow");
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 显示视频设备
     */
    public static void showVideoWindow() {
        readyClassLoaders();
        try {
            tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "showVideoWindow");
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
    
    /*
     * 关闭所有设备
     */
    public static void closeAll() throws Exception {
        readyClassLoaders();
        try {
            tool.invokeMethod(null, "JVideo2SWT.JVideoWindowControl", "disposeAll");
        } catch (Exception ex) {
            Logger.getLogger(DisplayManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
